#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <openssl/evp.h>
#define BUF_LEN 320000
int main(int argc, char* argv[])
{
  int rfd = open("/home/liu/123.eml", O_RDONLY);
  int wfd = open("/home/liu/123.jpg", O_WRONLY|O_CREAT,0666);
  unsigned char in_buf[BUF_LEN] = {0};
  unsigned char out_buf[BUF_LEN] = {0};
  int in_len = read(rfd, in_buf, BUF_LEN);
  printf("file len = [%d]\n", in_len);
  int total = 0;
  int out_len = 0;
  EVP_ENCODE_CTX  dctx;
  EVP_DecodeInit(&dctx);

  int ret = 0;
  while(1)
  {
    ret=EVP_DecodeUpdate(&dctx, out_buf, &out_len, in_buf, in_len);
    total += out_len;
    printf("ret = [%d]\n", ret);
    if ( ret <= 0)
    {
      break;
    }
  }

  ret = EVP_DecodeFinal(&dctx, out_buf, &out_len);
  total += out_len;

  write(wfd, out_buf, total);
  close(rfd);
  close(wfd);
  return 0;
}
